﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Adorners
{
    /// <summary>
    /// 用于点击添加同级标题。
    /// </summary>
    public class AddSameLevelTitleAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示标题被折叠状态的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public AddSameLevelTitleAdorner(UIElement adornedElement, Title masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.MouseEnter += new System.Windows.Input.MouseEventHandler(TitleCollapseAdorner_MouseEnter);
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(TitleCollapseAdorner_MouseLeave);
        }

        void TitleCollapseAdorner_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        void TitleCollapseAdorner_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        private Title masterTitle;

        public Title MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.masterTitle == null || masterTitle.BeCollapsed || masterTitle.IsFixed ||
                masterTitle.HigherLevelTitleID == null || masterTitle.HigherLevelTitleID.Length <= 0)
            {
                this.Visibility = Visibility.Hidden;
                return;
            }
            else
            {
                this.Visibility = Visibility.Visible;
            }

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);
            Rect newRect;

            if (Globals.MainWindow.View != ViewMode.Default || masterTitle.Direction == TitleDirection.Right)//只有导图会分左右。
            {
                newRect = new Rect((int)(adornedElementRect.X + masterTitle.ActualWidth - 12),
                              (int)(adornedElementRect.Y + adornedElementRect.Height / 2 + 8), 12, 12);
            }
            else
            {
                //在左侧时
                newRect = new Rect((int)(adornedElementRect.X - 20),
                              (int)(adornedElementRect.Y + adornedElementRect.Height / 2 + 8), 12, 12);
            }

            Pen linePen = null;

            if (this.IsMouseOver || this.masterTitle.IsSelected)
            {
                //画个小方框，带纵向矩形
                linePen = new Pen(Brushes.PaleVioletRed, 1);
                drawingContext.DrawRectangle(Brushes.Transparent, linePen, newRect);


                if (this.masterTitle.HigherLevelTitleID == Globals.MainWindow.RootTitle.ID && Globals.MainWindow.View == ViewMode.HorizontalList)
                {
                    //里面的小方块在：一级标题，且当前视图方式是横向列表时，须改为横向小方块。

                    drawingContext.DrawRectangle(Brushes.PaleVioletRed, linePen,
                    new Rect(newRect.Left + 3, newRect.Top + 5, 6, 2));
                }
                else
                {
                    drawingContext.DrawRectangle(Brushes.PaleVioletRed, linePen,
                    new Rect(newRect.Left + 5, newRect.Top + 3, 2, 6));
                }
            }
            else
            {
                //画个透明小方框
                linePen = new Pen(Brushes.Transparent, 1);
                drawingContext.DrawRectangle(Brushes.Transparent, linePen, newRect);
            }

            this.ToolTip = "点击添加同级标题";
        }
    }
}
